using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using E4D.AppFabric.Entities;
using Microsoft.ApplicationServer.Caching.AdminApi;

namespace MDCAdminTool
{
    public abstract class CacheEntityBase : ICacheCommand , INotifyPropertyChanged
    {
        #region Fields
        private string          _Name;
        private ServiceStatus   _Status;
        private CacheType       _CacheType;
        #endregion

        #region Properties
        public CacheType Type 
        {
            get { return _CacheType; }
            set { _CacheType = value; OnPropertyChanged( "Type" ); }
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = value; OnPropertyChanged( "Name" ); }
        }
        public ServiceStatus Status
        {
            get { return _Status; }
            set { _Status = value; OnPropertyChanged( "Status" ); }
        }
        public CacheEntityBase Parent { get; set; }
        #endregion

        protected void Log( object item )
        {
            try
            {
                Output.Add( item );
            }
            catch(  Exception ex )
            {
                var msg = ex.Message;
                Output.Add(msg);
            }
        }

        [Import]
        public ILogger Output { get; set; }

        #region ICacheCommand Members
        public virtual void Restart()
        {
            Stop();
            Start();
        }
        public virtual void Reload()
        {
            LoadConfig();
            LoadStatistics();
            LoadChildren();
        }
        public abstract void Start();
        public abstract void Stop();

        public abstract void Add(CacheEntityBase item );
        public abstract void Remove(CacheEntityBase item);

        public abstract CacheStats LoadStatistics();
        public abstract void LoadChildren();
        public abstract void LoadConfig();
        public abstract void SaveConfig();
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler  PropertyChanged;
        protected void OnPropertyChanged( string property )
        {
            if ( PropertyChanged != null )
                PropertyChanged( this , new PropertyChangedEventArgs( property ) );
        }

        #endregion
    }
}